//
//  loveTableViewCell.m
//  justCS
//
//  Created by wukai on 2018/6/14.
//  Copyright © 2018年 wukai. All rights reserved.
//

#import "loveTableViewCell.h"
#import "XMGWebVc.h"
@interface loveTableViewCell()

@end
@implementation loveTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}


- (void)setArray:(NSArray *)array{
    
    int maxCols = 4;
    CGFloat buttonW = ([UIScreen mainScreen].bounds.size.width-16) / maxCols;
    CGFloat buttonH = buttonW;
    
    XMGSqaureButton *senderH=nil;
    if (_array.count == 0) {
        for (int i=0; i<array.count; i++) {
            
            if (i ==0) {
                
                self.titleLable = [[UILabel alloc]init];
                self.titleLable.frame=CGRectMake(10, 10,XMGScreenW , 20);
                self.titleLable.textColor=[UIColor grayColor];
                [self.conentView addSubview: self.titleLable];
            }
            
            XMGSqaureButton *sender = [XMGSqaureButton buttonWithType:UIButtonTypeCustom];
            // 传递模型
            sender.square = array[i];
            // 监听点击
            [sender addTarget:self action:@selector(Tap:) forControlEvents:UIControlEventTouchDown];
            
//            sender.backgroundColor=[UIColor orangeColor];
            [self.conentView addSubview:sender];
           
            int col = i % maxCols;
            int row = i / maxCols;
            
            sender.x = col * buttonW;
            sender.y = 30+ row * buttonH;
            sender.width = buttonW;
            sender.height = buttonH;
            
            
            
            
            if (i ==array.count-1) {
                self.cellHeight = CGRectGetMaxY(sender.frame)+40;
            }
            senderH=sender;
        }
         
    }
    
    _array = array;
  
    
}


/**
 @param sender 按钮
 */

- (void)Tap:(XMGSqaureButton *)sender{
    
    if ([self.loveDelegate respondsToSelector:@selector(loveTableViewCellDelegate:)]) {
        [self.loveDelegate loveTableViewCellDelegate:sender];
    }
    return;
   
    XMGWebVc *web=[[XMGWebVc alloc]init];
    web.url=sender.square.url;
    web.title=sender.square.name;
    
   UIViewController *viewVc =  [UIApplication sharedApplication].keyWindow.rootViewController;
   UINavigationController *nav=(UINavigationController *)viewVc;
    [nav pushViewController:web animated:YES];
    
    
    NSLog(@" ===%@",sender.square.name);
    return;
    if (sender !=_selectBtn) {
        [self setLayerState:YES button:sender];
        [self setLayerState:NO button:self.selectBtn];
        _selectBtn = sender;
    }
    
    
}


/**
 @param state 选中状态
 @param sender 设置按钮
 */
- (void)setLayerState:(BOOL)state button:(UIButton *)sender{
    
    [sender.titleLabel setFont:[UIFont systemFontOfSize:16]];
    if(state){
        sender.selected = YES;
        sender.backgroundColor = [UIColor colorWithRed:248/255.0 green:160/255.0 blue:136/255.0 alpha:1.0];
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
    }else{
        sender.selected = NO;
        //设置圆角的半径
        [sender.layer setCornerRadius:3];
        //切割超出圆角范围的子视图
        sender.layer.masksToBounds = YES;
        //设置边框的颜色
        [sender.layer setBorderColor:[UIColor colorWithRed:147/255.0 green:185/255.0 blue:247/255.0 alpha:1.0].CGColor];
        //设置边框的粗细
        [sender.layer setBorderWidth:1.0];
        sender.backgroundColor = [UIColor whiteColor];
        [sender setTitleColor:[UIColor colorWithRed:147/255.0 green:185/255.0 blue:247/255.0 alpha:1.0] forState:UIControlStateNormal];
    }
    
}

@end
